//
//  PageContentView.m
//  PageTitleContent
//
//  Created by 好米有易 on 2017/9/13.
//  Copyright © 2017年 好米有易. All rights reserved.
//

#import "PageContentView.h"
#import "PageTitleView.h"
#import "TestCollectionViewCell.h"

@interface PageContentView ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, strong) UICollectionView *collectionView;

@property (nonatomic, strong) UICollectionViewFlowLayout *flowLayout;

// 当前的偏移量
@property (nonatomic, assign) CGFloat currentOffsetx;


/**
 是否接收到通知
 */
@property (nonatomic, assign) BOOL isReceiveNotice;

@end

static NSString *COLLECTION_ITEM_ID = @"COLLECTION_ITEM_ID";

@implementation PageContentView

- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    
    if (self) {
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(scrollAction:) name:@"ScrollActionNotification" object:nil];
        
        self.isReceiveNotice = NO;
        
        self.currentOffsetx = 0.f;
    }
    
    return self;
}

#pragma mark - lazy -
- (UICollectionViewFlowLayout *)flowLayout {
    
    if (_flowLayout == nil) {
        _flowLayout = [[UICollectionViewFlowLayout alloc] init];
        _flowLayout.itemSize = self.bounds.size;
        _flowLayout.minimumLineSpacing = 0;
        _flowLayout.minimumInteritemSpacing = 0;
        _flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    }
    
    return _flowLayout;
}

- (UICollectionView *)collectionView {
    if (_collectionView == nil) {
        _collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:self.flowLayout];
        _collectionView.bounces = NO;
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.pagingEnabled = YES;
        [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:COLLECTION_ITEM_ID];
    }
    return _collectionView;
}


#pragma mark - setter -
- (void)setTitles:(NSArray *)titles {
    _titles = titles;
    
    [self addSubview:self.collectionView];

}

- (void)setControllers:(NSArray<UIViewController *> *)controllers {
    
    _controllers = controllers;
    
    [self.collectionView reloadData];
}


#pragma mark - UICollectionViewDataSource -
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.titles.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:COLLECTION_ITEM_ID forIndexPath:indexPath];
    
    UIView *View = [self.controllers objectAtIndex:indexPath.row].view;

    
    [cell.contentView addSubview:View];
    
    return cell;
}

#pragma mark - UICollectionViewDelegate -
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    NSLog(@"scrollViewWillBeginDragging");
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    if (self.isReceiveNotice) { // 接收到通知返回
        return;
    }
    
   CGFloat floatValue = scrollView.contentOffset.x - self.currentOffsetx;
    
    CGFloat progress = floatValue / scrollView.bounds.size.width;
    
    NSDictionary *dict = @{@"progress":@(progress)};
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"ScrollNotification" object:nil userInfo:dict];
  
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    if (self.isReceiveNotice) { // 接收到通知返回
        return;
    }
    
    self.currentOffsetx = scrollView.contentOffset.x;
    
    NSDictionary *dict = @{@"index":@(scrollView.contentOffset.x / scrollView.bounds.size.width)};
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"RecordCurrentBtn" object:nil userInfo:dict];

    
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
    self.isReceiveNotice = NO;

}

#pragma mark - notification -

- (void)scrollAction:(NSNotification *)notice {
    
    self.isReceiveNotice = YES;
    
    NSDictionary *dict = [notice valueForKey:@"userInfo"];
    
    NSInteger index = [dict[@"index"] integerValue];
    
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
    
    [self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionNone animated:YES];
    
    self.currentOffsetx = index * self.collectionView.bounds.size.width;
    
    
}


@end
